<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/7/24
 * Time: 13:11
 */

namespace app\admin\controller;

use app\reuse\controller\ResponseJson;
use app\service\MemberService;

use app\service\OrderService;
use think\Db;


/**
 * 广告管理.
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月24日13:11:59
 */
class Member extends Common
{
    use ResponseJson;
    /**
     * 构造方法.
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();
        $this->IsLogin();
    }

    /**
     * [Index 用户列表]
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function Index()
    {


        if ('ajax' != input('get.action')) {

            $this->assign('cate', input('get.cate'));
            $this->assign('id_arr', input('get.id_arr'));
            return $this->fetch();
        } else {
            // 参数
            $params = input();

            if(!empty($params['id_arr'])&&$params['id_arr']>0){
                $params['param']['id_arr']= $params['id_arr'];
            }
            //dump($params['id_arr']>0);exit;
            // 条件
            $where = MemberService::MemberListWhere($params['param']);

            $data_params = [
                'where' => $where,
                'page' => true,
                'field' => '*',
                'number' => 10,
            ];

            $re = MemberService::MemberList($data_params);

            //($re);exit;
            // 总数
            $total = MemberService::MemberTotal($where);

            return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $re['data']];
        }
    }

    /**
     * [MemberDetail 用户详情]
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function MemberDetail(){
        if(input("get.action")=='ajax'){
            #修改用户信息
            $info = input("post.");
            $id = input("post.id");
            if(!empty($id)){
                Db::table("member")->where("id = $id")->update($info);
                return DataReturn('保存成功', 0);
            }

        }else{
            $params = input();

            // 获取列表
            $data_params = [
                'm' => 0,
                'n' => 1,
                'page' => false,
                'where' => ['id' => intval($params['id'])],
                'field' => '*',
                'table'=>'member'
            ];

            $ret =  MemberService::DataDealWith(MemberService::DataList($data_params),true);

            $data = empty($ret['data'][0]) ? [] : $ret['data'][0];
           // dump($data);exit;
            $this->assign('res', $data);
            $this->assign('id', $params['id']);

            return $this->fetch();
        }
    }
    /**
     * [saleMember 消费记录]
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    function saleMember(){

        $id = input("get.id");
        if(input("get.active") == "ajax"){
            if(!empty($id)){
                # 搜索条件
                $searchParams = input('post.searchParams');
                $order_where= "member_id = $id and pay_status = 1 and member_order.is_delete = 2 ";
                if (!empty($searchParams))
                {
                    # 商品名称 货号搜索
                    if (!empty($searchParams['search']))
                    {
                        $order_where .= " and order_number like '%" . $searchParams['search'] . "%' ";
                    }
                }
                # 定义listRows 分页
                $listRows = input('post.limit');
                if (empty($listRows)) {
                    $listRows = 10;
                }
                $order=Db::name('member_order')
                   // ->join('member_orderdetail','member_order.id=member_orderdetail.order_id')
                    ->where($order_where)
                    ->field('*')
                    ->paginate($listRows, false, ['query' => request()->param()]);
                return $this->jsonSuccessData($order);
            }
        }else{
            $this->assign('id',$id);
            return $this->fetch();
        }
    }

    /**
     * [MemberDel 删除用户]
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function MemberDel(){
        $params=$this->data_post;
        $params['table']='member';
        $params['soft']=true;
        $params['soft_arr']=['is_delete'=>1];
        $params['errorcode']=60001;
        $params['msg']='用户删除失败';
        MemberService::DelInfo($params);
        return DataReturn('删除成功', 0);
    }

    /**
     * [SaleDetail 用户订单详情]
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function SaleDetail()
    {
        $params = $this->data_get;
        // 公共数据校验请求参数
        $p = [
            [
                'checked_type' => 'empty',
                'key_name' => 'id',
                'error_msg' => 'id不能为空',
                'error_code' => 50001,
            ],
        ];
        $ret=ParamsChecked($params, $p);
        if(true !== $ret)
        {
            $error_arr = explode(',', $ret);
            throw new BaseException(['code'=>403 ,'errorCode'=>$error_arr[1],'msg'=>$error_arr[0],'status'=>false,'debug'=>false ]);
        }
        $field='mo.id mo_id,mod.express_code,mod.logistic_id,mod.id mod_id,mo.receive_phone,mo.receive_name,mo.member_id,mo.province,mo.city,mo.area,mo.address,mo.pay_price,mo.cheap_price,mo.total_price,mo.pay_type,mo.pay_time,mo.order_number';
        // 获取列表
        $data_params = [
            'page' => false,
            'where' => ['mo.id' => intval($params['id'])],
            'field'=>$field,

        ];
        $ret=OrderService::OrderList($data_params);
       //dump($ret);exit;
        $this->assign('res',$ret['data'][0]);

        return $this->fetch();
    }
}